/*
 * Copyright (c) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.google.api.client.sample.gan;

import com.google.api.services.gan.*;
import com.google.api.services.gan.model.*;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.services.samples.shared.cmdline.oauth2.LocalServerReceiver;
import com.google.api.services.samples.shared.cmdline.oauth2.OAuth2Native;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.api.client.http.javanet.NetHttpTransport;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Queries the GAN API for credit card, advertiser, publisher and event data.
 * See for GAN API documentation: <a
 * href="https://code.google.com/apis/gan/index.html">Google Affiliate Network
 * Documentation</a>
 * 
 * @author apasha@google.com (Ali Pasha)
 */
public class GanSample {
  /** Global instance of the HTTP transport. */
  private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

  /** Global instance of the JSON factory. */
  private static final JsonFactory JSON_FACTORY = new JacksonFactory();

  /** OAuth 2.0 scopes. */
  private static final List<String> SCOPES = Arrays
      .asList("https://www.googleapis.com/auth/gan");

  private static Gan server;

  public static void main(String[] args) throws Exception {
    Params params = Params.parse(args);

    if (params == null) {
      Params.printUsage();
      return;
    }

    // authorization
    Credential credential = OAuth2Native.authorize(HTTP_TRANSPORT,
        JSON_FACTORY, new LocalServerReceiver(), SCOPES);

    server = new Gan.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
        .setApplicationName("Google-OAuth2Sample/1.0").build();

    switch (params.getType()) {
    case CC_OFFERS:
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      System.out.println("Credit Card Offers Api");
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      queryCcOfferApi(params);
      break;
    case EVENTS:
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      System.out.println("Event Api");
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      queryEventApi(params);
      break;
    case ADVERTISERS:
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      System.out.println("Advertiser Api List");
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      queryAdvertiserApi(params);
      break;
    case PUBLISHERS:
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      System.out.println("Publisher Api List");
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      queryPublisherApi(params);
      break;
    case LINKS:
      System.out
        .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      System.out.println("Links Api List");
      System.out
        .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      queryLinkApi(params);
    break;
    case ADVERTISER:
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      System.out.println("Advertiser Api Get/Lookup");
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      queryAdvertiserLookupApi(params);
      break;
    case PUBLISHER:
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      System.out.println("Publisher Api Get/Lookup");
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      queryPublisherLookupApi(params);
      break;
    case LINK:
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      System.out.println("Link Api Get/Lookup");
      System.out
          .println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
      queryLinkLookupApi(params);
      break; 	
    }
  }

  private static void queryLinkApi(Params params) throws Exception {
    // Set up request
    Gan.Links.List request = server.links().list(params.getRole(), params.getRoleId());

    // Execute request
    List<Link> results = request.execute().getItems();
    if (results == null) {
      System.out.println("No Results Returned!");
    } else {
      for (Link c : results) {
        System.out.println(c);
      }
    }
  }

  private static void queryLinkLookupApi(Params params) throws Exception {
    // Set up request
    Gan.Links.Get request = server.links().get(params.getRole(),
        params.getRoleId(), Long.valueOf(params.getLookupId()));

    // Execute request
    Link result = request.execute();
    if (result == null) {
      System.out.println("No Result Returned!");
    } else {
      System.out.println(result);
    }
  }

  private static void queryAdvertiserLookupApi(Params params)
      throws Exception {
    // Set up request
    Gan.Advertisers.Get request = server.advertisers().get(
        params.getRole(), params.getRoleId());
    if (params.hasLookupId()) {
      request.setAdvertiserId(params.getLookupId());
    }

    // Execute request
    Advertiser result = request.execute();
    if (result == null) {
      System.out.println("No Result Returned!");
    } else {
      System.out.println(result);
    }
  }

  private static void queryAdvertiserApi(Params params) throws Exception {
    // Set up request
    Gan.Advertisers.List request = server.advertisers().list(
        params.getRole(), params.getRoleId());
    request.setAdvertiserCategory("travel");

    // Execute request
    List<Advertiser> results = request.execute().getItems();
    if (results == null) {
      System.out.println("No Results Returned!");
    } else {
      for (Advertiser a : results) {
        System.out.println(a);
      }
    }
  }

  private static void queryCcOfferApi(Params params) throws Exception {
    // Set up request
    Gan.CcOffers.List request = server.ccOffers().list(params.getRoleId());

    // Execute request
    List<CcOffer> results = request.execute().getItems();
    if (results == null) {
      System.out.println("No Results Returned!");
    } else {
      for (CcOffer c : results) {
        System.out.println(c);
      }
    }
  }

  private static void queryEventApi(Params params) throws Exception {
    // Set up request
    Gan.Events.List request = server.events().list(params.getRole(),
        params.getRoleId());
    request.setEventDateMin("2011-08-11T04:11:42.000Z");

    // Execute request
    List<Event> results = request.execute().getItems();
    System.out.println("Not filtered: ");
    if (results == null) {
      System.out.println("No Results Returned!");
    } else {
      for (Event e : results) {
        System.out.println("Advertiser ID: " + e.getAdvertiserId()
            + " Order ID: " + e.getOrderId() + " Event Date: "
            + e.getEventDate());
      }
    }
  }

  private static void queryPublisherApi(Params params) throws Exception {
    // Set up request
    Gan.Publishers.List request = server.publishers().list(
        params.getRole(), params.getRoleId());
    request.setPublisherCategory("community_and_content");

    // Execute request
    List<Publisher> results = request.execute().getItems();
    if (results == null) {
      System.out.println("No Results Returned!");
    } else {
      for (Publisher p : results) {
        System.out.println(p);
      }
    }
  }

  private static void queryPublisherLookupApi(Params params) throws Exception {
    // Set up request
    Gan.Publishers.Get request = server.publishers().get(params.getRole(),
        params.getRoleId());
    if (params.hasLookupId()) {
      request.setPublisherId(params.getLookupId());
    }

    // Execute request
    Publisher result = request.execute();
    if (result == null) {
      System.out.println("No Result Returned!");
    } else {
      System.out.println(result);
    }
  }
}
